<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ include file="/page/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${appid}-应用源码工程</title>
<%@ include file="/adapter/adapter-macms.jsp" %>
<%@ include file="/adapter/adapter-easyui.jsp" %>
<link rel="stylesheet" href="${ctx}/res/common/css/zTreeStyle.css" type="text/css"></link>
<script src="${ctx}/res/common/js/jquery.min.js" type="text/javascript"></script>
<script src="${ctx}/res/common/js/jquery.ztree.core-3.5.js" type="text/javascript"></script>
<script src="${ctx}/res/common/edit_area/edit_area_full.js" type="text/javascript"></script>
<script src="/macms/assets/jquery-mask.js" type="text/javascript"></script>
<script type="text/javascript" >	
	var zTree;
	var demoIframe;

	var setting = {
		view: {
			dblClickExpand: true,
			showLine: true,
			selectedMulti: false
		},
		data: {
			simpleData: {
				enable:true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: ""
			}
		},
		callback: {
			beforeClick: function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);
					return false;
				} else {
					var filePath = treeNode.file;
					var fileName = treeNode.name;
					var fileType= "";
					if(fileName.indexOf(".html")>0){
						fileType="html";
					}else if(fileName.indexOf(".css")>0){
						fileType="css";
					}else if(fileName.indexOf(".js")>0){
						fileType="js";
					}else if(fileName.indexOf(".xml")>0){
						fileType="xml";
					}else if(fileName.indexOf(".png")>0||fileName.indexOf(".gif")>0||fileName.indexOf(".jar")>0||fileName.indexOf(".apk")>0){
						alert("该类型的文件不支持编辑，无法打开");
						return true;
					}else{
						fileType="php";
					}
					openFile(filePath,fileName,fileType);
					return true;
				}
			}
		}
	};
	

	$(function() {
		$(document).mask('正在准备应用工程代码...');
		$.ajax( {
			url : "${ctx}/appProject/projectFileList?appid=${appid}",
			type : "POST",
			dataType : "json",
			async : true,
			success : function(data) {
				var zNodes = eval(data.model.jsonStr);
				var t = $("#menu_app_config");
				t = $.fn.zTree.init(t, setting, zNodes);
				var zTree = $.fn.zTree.getZTreeObj("menu_app_config");
				zTree.expandNode(zTree.getNodeByParam("id", 1));
				$(document).unmask();
			},
			error : function() {
				alert("error");
			}
		});
	});
	editAreaLoader
			.init( {
				id : "code_area", // id of the textarea to transform	
				start_highlight : true,
				allow_toggle : false,
				word_wrap : true,
				language : "en",
				syntax : "html",
				toolbar : "save,|,search,|,go_to_line, |, undo, redo, |, select_font, |, syntax_selection,|, help",
				syntax_selection_allow : "css,html,js,php,python,vb,xml,c,cpp,sql,basic,pas,brainfuck",
				is_multi_files : true,
				load_callback : "editAreaLoaded",
				save_callback : "my_save",
				show_line_colors : true
			});
	function editAreaLoaded(id) {

	}
	function my_save(id, content) {
		var file = editAreaLoader.getCurrentFile(id);
		if (file == null) {
			return;
		}
		$.ajax( {
			url : "${ctx}/appProject/savefile",
			type : "POST",
			data : {
				path : file.id,
				content : content
			},
			dataType : "json",
			async : true,
			success : function(data) {
				var flag = data.flag;
				if (flag == 'success') {
					//editAreaLoader.setValue(editor_id, content);
			//var fileId = file.id+"";
			//var id = fileId.replace(":","%3A");
			//id = fileId.replace("\\","%5C");
			//alert(id);
			//alert($("#tab_file_"+id));
			alert("保存成功");
		} else {
			alert("保存文件失败");
		}
	},
	error : function() {
		alert("系统繁忙，保存文件失败");
	}
		});
	}
	function openFile(filePath, fileName, fileType) {
		$.ajax( {
			url : "${ctx}/appProject/openfile?file=" + filePath,
			type : "POST",
			dataType : "json",
			async : false,
			success : function(data) {
				var content = data.content;
				var new_file = {
					id : filePath,
					text : content,
					syntax : fileType,
					title : fileName
				};
				editAreaLoader.openFile('code_area', new_file);
			},
			error : function() {
				alert("系统繁忙，打开文件失败");
			}
		});
	}
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'west',title:'应用源代码'" style="width:233px;padding:1px;">
	<ul class="ztree" data-options="animate:true,lines:true" id="menu_app_config">
	</ul>
</div>
<div data-options="region:'center',title:'代码编辑区域'">
<fieldset style="height: 100%; width: 100%;">
		<textarea id="code_area" style="height: 100%; width: 100%;" name="test_2"></textarea>
	</fieldset>
</div>
</body>
</html>